import gtk
import clutter
import filepicker
import encode
import threading
import logging
import ui

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('runners.py')
handler = logging.StreamHandler()
formatter = logging.Formatter("[%(module)s][%(funcName)s] - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)

def basic_encoding():
    encoder = encode.Encoder()
    
    global encoder
    encoder.set_input_filename('/home/nick/Examples/Ubuntu_Free_Culture_Showcase/StopMotionUbuntu.ogv')
    encoder.set_output_dimensions(320, 200)
    encoder.encode()

def threaded_encoding():
    encoder = encode.Encoder()
    
    global encoder
    encoder.set_input_filename('/home/nick/Examples/Ubuntu_Free_Culture_Showcase/StopMotionUbuntu.ogv')
    encoder.set_output_dimensions(320, 200)
    
    def do_encode():
        logger.info( 'do_encode start' )
        encoder.encode()
        logger.info (  'do_encode done' )
    
    thread = threading.Thread(target=do_encode)
    logger.info( 'threaded_encoding starting thread' )
    thread.start()
    logger.info( 'threaded_encoding thread started' )
    thread.join()
    logger.info('joined encoder thread')
    
def encode_with_gui():
    gtk.threads_init()
    encoder = encode.Encoder()
    observer = ui.EncoderProgressMonitor()
    observer.show()
    
    encoder.set_input_filename('/home/nick/Examples/Ubuntu_Free_Culture_Showcase/StopMotionUbuntu.ogv')
    encoder.set_output_dimensions(320, 200)
    encoder.add_output_observer(observer)
    def do_encode():
        encoder.encode()
    
#    thread = threading.Thread(target=do_encode)
#    thread.start()

    def run_gtk():
        gtk.main()
        
    gtkthread = threading.Thread(target=run_gtk)
    gtkthread.start()
    
    encoder.encode()

if __name__ == '__main__':
#    basic_encoding()
#    threaded_encoding()
    encode_with_gui()    
    
#chooser.connect('selection', get_selected)
#clutter.main()